﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;

namespace XiaoGang.Soft.XGWMS.COM
{
    public sealed class UtilityLog
    {
        private static string strClsName = "UtilityLog";
        //private static string vProgramName = "DemoSystemService";
        private static log4net.ILog logger = log4net.LogManager.GetLogger(strClsName);

        #region  Method

        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        private static string GetCustomerIP()
        {
            string CustomerIP = "IP";
            //System.Web.Services.WebService webservice = new System.Web.Services.WebService();
            //string CustomerIP = "";
            //if (webservice.Context.Request.ServerVariables["HTTP_VIA"] != null)
            //{
            //    CustomerIP = webservice.Context.Request.ServerVariables["HTTP_X_FORWARDED_FOR"].ToString();
            //}
            //else
            //{
            //    CustomerIP = webservice.Context.Request.ServerVariables["REMOTE_ADDR"].ToString();
            //}
            return CustomerIP;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="logger"></param>
        /// <param name="strMess"></param>
        /// <returns></returns>
        public static bool WriteLog4Net(log4net.ILog logger, string strMess)
        {
            try
            {
                logger.Info(strMess);
                return true;
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="enumStyle"></param>
        public static void WriteLogWithLog4Net(COM.enumDbLog enumStyle)
        {
            string strCustomerIP = GetCustomerIP();
            string strContent = string.Empty;
            switch (enumStyle)
            {
                case enumDbLog.Connect:
                    strContent = "[1] [" + strCustomerIP + "] Connect";
                    break;
                case enumDbLog.Close:
                    strContent = "[3] [" + strCustomerIP + "] Close";
                    break;
                case enumDbLog.Excute:
                    strContent = "[4] [" + strCustomerIP + "] Excute";
                    break;
                case enumDbLog.BeginTransaction:
                    strContent = "[5] [" + strCustomerIP + "] BeginTransaction";
                    break;
                case enumDbLog.CommitTransaction:
                    strContent = "[6] [" + strCustomerIP + "] CommitTransaction";
                    break;
                case enumDbLog.RollbackTransaction:
                    strContent = "[7] [" + strCustomerIP + "] RollbackTransaction";
                    break;
                default:
                    break;
            }
            logger.Info(strContent);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="InfoMsg"></param>
        public static void WriteInfoLogWithLog4Net(string InfoMsg)
        {
            string strCustomerIP = GetCustomerIP();
            logger.Info("[0] [" + strCustomerIP + "] " + InfoMsg);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strMsg"></param>
        public static void WriteLogWithLog4Net(string strMsg)
        {
            string strCustomerIP = GetCustomerIP();
            logger.Info("[2] [" + strCustomerIP + "] " + strMsg);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="strErrMsg"></param>
        public static void WriteErrLogWithLog4Net(string strErrMsg)
        {
            string strCustomerIP = GetCustomerIP();
            logger.Error("[9] [" + strCustomerIP + "] " + strErrMsg);
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="ex"></param>
        public static void WriteErrLogWithLog4Net(Exception ex)
        {
            WriteErrLogWithLog4Net(ex.Message + ex.StackTrace);
        }

        #endregion
    }
}
